package org.loofer.ext.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.Proxy;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build.VERSION;
import android.support.annotation.RequiresPermission;
import android.text.TextUtils;
import org.loofer.ext.Ext;
import org.loofer.ext.component.logger.Logger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap;

public final class NetWorkUtils {
    public static final String APN_NAME_WIFI = "wifi";
    public static final int NETWORK_TYPE_2G = 3;
    public static final int NETWORK_TYPE_3G = 4;
    public static final int NETWORK_TYPE_4G = 5;
    public static final int NETWORK_TYPE_NONE = -1;
    public static final int NETWORK_TYPE_UNKNOWN = 1;
    public static final int NETWORK_TYPE_UNKNOWN_MOBILE = 2;
    public static final int NETWORK_TYPE_WIFI = 10;
    private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
    private static final String TAG = "NetworkUtil";
    private static final HashMap<String, NetworkProxy> sAPNProxies = new HashMap();

    public static final class DNS {
        public String primary;
        public String secondary;

        DNS() {
        }

        public String toString() {
            return this.primary + "," + this.secondary;
        }
    }

    public static class NetworkProxy implements Cloneable {
        public final String host;
        public final int port;

        NetworkProxy(String host, int port) {
            this.host = host;
            this.port = port;
        }

        final NetworkProxy copy() {
            try {
                return (NetworkProxy) clone();
            } catch (CloneNotSupportedException e) {
                return null;
            }
        }

        public String toString() {
            return this.host + ":" + this.port;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj != null && (obj instanceof NetworkProxy)) {
                NetworkProxy proxy = (NetworkProxy) obj;
                if (TextUtils.equals(this.host, proxy.host) && this.port == proxy.port) {
                    return true;
                }
            }
            return false;
        }
    }

    static {
        sAPNProxies.put("cmwap", new NetworkProxy("10.0.0.172", 80));
        sAPNProxies.put("3gwap", new NetworkProxy("10.0.0.172", 80));
        sAPNProxies.put("uniwap", new NetworkProxy("10.0.0.172", 80));
        sAPNProxies.put("ctwap", new NetworkProxy("10.0.0.200", 80));
    }

    public static boolean isNetworkConnected() {
        return isNetworkConnected(Ext.getContext());
    }

    public static boolean isNetworkConnected(Context context) {
        NetworkInfo info = getActiveNetworkInfo(context);
        return info != null && info.isConnected();
    }

    public static boolean isWifiConnected() {
        return isWifiConnected(Ext.getContext());
    }

    public static boolean isWifiConnected(Context context) {
        boolean z = true;
        if (context == null) {
            return false;
        }
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo == null || activeNetworkInfo.getType() != 1) {
            z = false;
        }
        return z;
    }

    public static boolean isMobileConnected(Context context) {
        if (context == null) {
            return false;
        }
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo == null || activeNetworkInfo.getType() != 0) {
            return false;
        }
        return true;
    }

    public static NetworkInfo getActiveNetworkInfo(Context context) {
        try {
            return ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
        } catch (Throwable e) {
            Logger.e(TAG, "fail to get active network info", e);
            return null;
        }
    }

    public static int getActiveNetworkType(Context context) {
        return getNetworkType(context, getActiveNetworkInfo(context));
    }

    public static int getNetworkType(Context context, NetworkInfo info) {
        if (info == null) {
            return -1;
        }
        if (info.getType() == 1) {
            return 10;
        }
        if (info.getType() != 0) {
            return 1;
        }
        switch (info.getSubtype()) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                return 3;
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 15:
                return 4;
            case 13:
            case 14:
                return 5;
            default:
                return 2;
        }
    }

    public static NetworkProxy getProxy(Context context, boolean apnProxy) {
        return !apnProxy ? getProxy(context) : getProxyByAPN(context);
    }

    public static NetworkProxy getProxy(Context context) {
        if (!isMobileConnected(context)) {
            return null;
        }
        String proxyHost = getProxyHost(context);
        int proxyPort = getProxyPort(context);
        if (isEmpty(proxyHost) || proxyPort < 0) {
            return null;
        }
        return new NetworkProxy(proxyHost, proxyPort);
    }

    private static String getProxyHost(Context context) {
        if (VERSION.SDK_INT < 11) {
            return Proxy.getDefaultHost();
        }
        return System.getProperty("http.proxyHost");
    }

    private static int getProxyPort(Context context) {
        int port = -1;
        if (VERSION.SDK_INT < 11) {
            port = Proxy.getDefaultPort();
        } else {
            String portStr = System.getProperty("http.proxyPort");
            if (!isEmpty(portStr)) {
                try {
                    port = Integer.parseInt(portStr);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        if (port < 0 || port > 65535) {
            return -1;
        }
        return port;
    }

    public static NetworkProxy getProxyByAPN(Context context) {
        if (!isMobileConnected(context)) {
            return null;
        }
        NetworkProxy proxy = (NetworkProxy) sAPNProxies.get(getAPN(context));
        if (proxy != null) {
            return proxy.copy();
        }
        return null;
    }

    public static String getAPN(Context context) {
        NetworkInfo activeNetInfo = getActiveNetworkInfo(context);
        if (activeNetInfo == null) {
            return null;
        }
        String apn = null;
        if (activeNetInfo.getType() == 1) {
            apn = APN_NAME_WIFI;
        } else if (activeNetInfo.getType() == 0) {
            if (VERSION.SDK_INT <= 16) {
                Cursor cursor = null;
                try {
                    cursor = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
                    while (cursor != null && cursor.moveToNext()) {
                        apn = cursor.getString(cursor.getColumnIndex("apn"));
                    }
                    if (cursor != null) {
                        cursor.close();
                    }
                } catch (Throwable th) {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
            if (TextUtils.isEmpty(apn)) {
                apn = activeNetInfo.getExtraInfo();
            }
        }
        if (apn != null) {
            return apn.toLowerCase();
        }
        return apn;
    }

    @RequiresPermission("android.permission.ACCESS_WIFI_STATE")
    public static DNS getDNS(Context context) {
        DNS dns = new DNS();
        if (context != null && isWifiConnected(context)) {
            DhcpInfo dhcpInfo = ((WifiManager) context.getSystemService(APN_NAME_WIFI)).getDhcpInfo();
            if (dhcpInfo != null) {
                dns.primary = int32ToIPStr(dhcpInfo.dns1);
                dns.secondary = int32ToIPStr(dhcpInfo.dns2);
            }
        }
        if (dns.primary == null && dns.secondary == null) {
            dns.primary = PropertyUtils.get(PropertyUtils.PROPERTY_DNS_PRIMARY, null);
            dns.secondary = PropertyUtils.get(PropertyUtils.PROPERTY_DNS_SECONDARY, null);
        }
        return dns;
    }

    private static String int32ToIPStr(int ip) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(ip & 255).append(".");
        buffer.append((ip >> 8) & 255).append(".");
        buffer.append((ip >> 16) & 255).append(".");
        buffer.append((ip >> 24) & 255);
        return buffer.toString();
    }

    public static String getLocalIP() {
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                Enumeration<InetAddress> enumIpAddr = ((NetworkInterface) en.nextElement()).getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (Throwable th) {
        }
        return null;
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}
